<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>菜单</title>
<link href="<%=request.getContextPath()%>/css/Default.css"
	rel="stylesheet" type="text/css" />
<link href="<%=request.getContextPath()%>/css/ui.jqgrid.css"
	rel="stylesheet" type="text/css"></link>
<link
	href="<%=request.getContextPath()%>/css/redmond/jquery-ui-1.8.14.custom.css"
	rel="stylesheet" type="text/css"></link>
<link href="<%=request.getContextPath()%>/css/Dialog.css"
	rel="stylesheet" type="text/css"></link>
<script src="<%=request.getContextPath()%>/scripts/jquery-1.5.1.min.js"
	type="text/javascript"></script>
<script src="<%=request.getContextPath()%>/scripts/HRM.js"
	type="text/javascript"></script>
<script
	src="<%=request.getContextPath()%>/scripts/jqgrid/i18n/grid.locale-cn.js"
	type="text/javascript"></script>
<script
	src="<%=request.getContextPath()%>/scripts/jqgrid/jquery.jqGrid.src.js"
	type="text/javascript"></script>
<script
	src="<%=request.getContextPath()%>/scripts/jquery-ui-1.8.14.min.js"
	type="text/javascript"></script>
<script src="<%=request.getContextPath()%>/scripts/jquery.form.js"
	type="text/javascript"></script>
<script src="<%=request.getContextPath()%>/scripts/Dialog.js"
	type="text/javascript"></script>
<script src="<%=request.getContextPath()%>/scripts/DataGrid.js"
	type="text/javascript"></script>
<script>
$().ready(function() {//加载用户列表
	init();
		$(".menuTrImg").die('click').live('click',function(){
			var oldSrc=$(this).attr('src');
			var newSrc="";
			var id=$(this).attr('id');
			if($(this).attr('status')=='open'){
				$(this).attr('status','close');
				newSrc=oldSrc.replace("butExpand","butCollapse");
				$('.actionTr').each(function(index,obj){
					var parentId=$(this).attr('parentImg');
					if(parentId==id){
						$(this).hide();
					}
				});
			}else{
				$(this).attr('status','open');
				newSrc=oldSrc.replace("butCollapse","butExpand");
				$('.actionTr').each(function(index,obj){
					var parentId=$(this).attr('parentImg');
					if(parentId==id){
						$(this).show();
					}
				});
			}
			$(this).attr('src',newSrc);
		});
		$(".menuTrCheckbox").die('click').live('click',function(){
			var trImageId=$(this).prev().attr('id');
			if($(this).attr('checked')){//该状态为点击后的checkbox选中状态
				$(".actionTr").each(function(index,obj){
					if($(this).attr('parentimg')==trImageId){
						$(this).find(".actionTrCheckbox").attr('checked','true');
					}
				});
			}else{
				$(".actionTr").each(function(index,obj){
					if($(this).attr('parentimg')==trImageId){
						$(this).find(".actionTrCheckbox").removeAttr('checked');
					}
				});
			}
		});
		
	});
	$("#newMenuBtn").die('click').live('click',function(){
		var params={
				  'title':'新增菜单',
				  'url':$(this).attr('url'),
				  'width':550,
				  'height':410,
				  btnSucFunc:function(){
					  window.location.reload();
				  }
				 };
		createIframeDialog(params);
	});
	$('#editMenuBtn').die('click').live('click',function(){
		var id=-1; 
		var isActionSelected=false;
		$(".actionTrCheckbox").each(function(index,obj){
			if($(obj).attr('checked')){
				id=$(obj).val();
				isActionSelected=true;
			}
		});
		$(".menuTrCheckbox").each(function(index,obj){
			if($(obj).attr('checked')){
				id=$(obj).val();
			}
		});
		if(id<=0){
			createAlertDialog('提示信息','请选择一个菜单进行编辑!');
			return false;
		}
		var params;
		if(isActionSelected){
			params={
				  'title':'编辑菜单',
				  'url':$(this).attr('url')+'&actionId='+id,
				  'width':550,
				  'height':410,
				  btnSucFunc:function(){
					  window.location.reload();
				  }
				 };
		}else{
			params={
				  'title':'编辑菜单',
				  'url':$(this).attr('url')+'&menuId='+id,
				  'width':450,
				  'height':310,
				  btnSucFunc:function(){
					  window.location.reload();
				  }
				 };
		}
		createIframeDialog(params);
	});
	$('#delMenuBtn').die('click').live('click',function(){
		var id=-1; 
		var menuName="";
		var isMenuSelected=false;
		$(".actionTrCheckbox").each(function(index,obj){
			if($(obj).attr('checked')){
				id=$(obj).val();
				menuName=$(obj).parent().next().text().trim();
			}
		});
		$(".menuTrCheckbox").each(function(index,obj){
			if($(obj).attr('checked')){
				isMenuSelected=true;
			}
		});
		if(isMenuSelected){
			createAlertDialog('提示信息','一级菜单只能隐藏,不能删除!');
			return false;
		}
		if(id<=0){
			createAlertDialog('提示信息','请选择一个菜单进行删除!');
			return false;
		}
		
		var msg='确认删除<span style="color:red;font-weight:bold;">'+menuName+'</span>菜单?';
		createConfirmDialog('确认删除菜单?',msg,function(){
			$.ajax({
				type : "GET",
				url : '<%=request.getContextPath()%>/menu.do?method=deleteActionById&id='+id,
				dataType : "json",
				success : function(data) {
					if (data['status'] == 'ERROR') {
						createAlertDialog('提示信息','删除菜单失败!');
					} else {
						createAlertDialog('提示信息','删除菜单成功!',function(){
							window.location.reload();
						});
					}
				},
				error : function() {
					createAlertDialog('提示信息','网络繁忙,请稍后再试!');
				}
			});
		});
	});
	$('#hideMenuBtn').die('click').live('click',function(){
		var id=-1; 
		var menuName="";
		var isMenuSelected=false;
		var methodName="";
		var isHide=false;
		$(".actionTrCheckbox").each(function(index,obj){
			if($(obj).attr('checked')){
				id=$(obj).val();
				menuName=$(obj).parent().next().text().trim();
				methodName="hideActionById";
				if(!$(obj).parent().siblings().find("input:checkbox").attr("checked")){
					isHide=true;
				}
			}
		});
		$(".menuTrCheckbox").each(function(index,obj){
			if($(obj).attr('checked')){
				isMenuSelected=true;
				id=$(obj).val();
				menuName=$(obj).parent().next().text().trim();
				methodName="hideMenuById";
				if(!$(obj).parent().siblings().find("input:checkbox").attr("checked")){
					isHide=true;
				}
			}
		});
		if(isHide){
			createAlertDialog('提示信息','该菜单当前已为隐藏状态!');
			return false;
		}
		if(id<=0){
			createAlertDialog('提示信息','请选择一个菜单进行隐藏!');
			return false;
		}
		
		var msg='确认隐藏<span style="color:red;font-weight:bold;">'+menuName+'</span>菜单?';
		if(isMenuSelected){
			msg+='说明:隐藏一级菜单时会同时隐藏其子菜单!';
		}
		createConfirmDialog('确认隐藏菜单?',msg,function(){
			$.ajax({
				type : "GET",
				url : '<%=request.getContextPath()%>/menu.do?method='+methodName+'&id='+id,
				dataType : "json",
				success : function(data) {
					if (data['status'] == 'ERROR') {
						createAlertDialog('提示信息','隐藏菜单失败!');
					} else {
						createAlertDialog('提示信息','隐藏菜单成功!',function(){
							window.location.reload();
						});
					}
				},
				error : function() {
					createAlertDialog('提示信息','网络繁忙,请稍后再试!');
				}
			});
		},340,220);
	});
	$('#showMenuBtn').die('click').live('click',function(){
		var id=-1; 
		var menuName="";
		var isMenuSelected=false;
		var methodName="";
		var isShow=false;
		$(".actionTrCheckbox").each(function(index,obj){
			if($(obj).attr('checked')){
				id=$(obj).val();
				menuName=$(obj).parent().next().text().trim();
				methodName="showActionById";
				if($(obj).parent().siblings().find("input:checkbox").attr("checked")){
					isShow=true;
				}
			}
		});
		$(".menuTrCheckbox").each(function(index,obj){
			if($(obj).attr('checked')){
				isMenuSelected=true;
				id=$(obj).val();
				menuName=$(obj).parent().next().text().trim();
				methodName="showMenuById";
				if($(obj).parent().siblings().find("input:checkbox").attr("checked")){
					isShow=true;
				}
			}
		});
		if(isShow){
			createAlertDialog('提示信息','该菜单当前已为可见状态!');
			return false;
		}
		if(id<=0){
			createAlertDialog('提示信息','请选择一个菜单进行显示!');
			return false;
		}
		
		var msg='确认显示<span style="color:red;font-weight:bold;">'+menuName+'</span>菜单?';
		if(isMenuSelected){
			msg+='说明:显示一级菜单时会同时显示其子菜单!';
		}
		createConfirmDialog('确认显示菜单?',msg,function(){
			$.ajax({
				type : "GET",
				url : '<%=request.getContextPath()%>/menu.do?method='+methodName+'&id='+id,
				dataType : "json",
				success : function(data) {
					if (data['status'] == 'ERROR') {
						createAlertDialog('提示信息','显示菜单失败!');
					} else {
						createAlertDialog('提示信息','显示菜单成功!',function(){
							window.location.reload();
						});
					}
				},
				error : function() {
					createAlertDialog('提示信息','网络繁忙,请稍后再试!');
				}
			});
		},340,220);
	});
	function init() {
		$("#menuData tr").not(".dataTableHead").remove();
		var path='<%=request.getContextPath()%>/menu.do?method=getMenus';
		$.getJSON(path,function(data,status,xhr){
			data=data[0];
			var lastActionNum=0;
			for(var i=0;i<data.length;i++){
				var menuTrHtml="";
				if((i+1+lastActionNum)%2!=0){
					menuTrHtml+="<tr rowno='"+(i+1+lastActionNum)+"' style='background-color:#F9FBFC'>";
				}else{
					menuTrHtml+="<tr rowno='"+(i+1+lastActionNum)+"' style='background-color:#FFFFFF'>";
				}
				menuTrHtml+="<td>"+(i+1+lastActionNum)+"</td>";
				menuTrHtml+="<td><input	class='menuTrCheckbox' type='checkbox' value='"+data[i]['id']+"'/></td>";
				menuTrHtml+="<td><img id='menuImg_"+data[i]['id']+"' class='menuTrImg' status='open' src='<%=request.getContextPath()%>/images/butExpand.gif'/>&nbsp;";
				menuTrHtml+="<img align='absmiddle'	src='<%=request.getContextPath()%>/images/icons/icon003a1.gif'>&nbsp;"+data[i]['menuName']+"</td>";
				menuTrHtml+="<td></td>";//菜单编码
				if(data[i]['viewMode']=='Y'){//是否可见
					menuTrHtml+="<td><input	type='checkbox' value='"+data[i]['id']+"' disabled checked/></td>";
				}else{
					menuTrHtml+="<td><input	type='checkbox' value='"+data[i]['id']+"' disabled /></td>";
				}
				menuTrHtml+="<td></td>";//ICON
				menuTrHtml+="<td></td>";//URL
				menuTrHtml+="<td>"+data[i]['refreshTime']+"</td>";//URL
				menuTrHtml+="<td>"+data[i]['remark']+"</td>";//REMARK
				menuTrHtml+="</tr>";
				for(var j=0;j<data[i]['actions'].length;j++){
					var actionData=data[i]['actions'][j];
					var actionTrHtml="";
					if((i+j+2+lastActionNum)%2!=0){
						actionTrHtml+="<tr rowno='"+(i+j+2+lastActionNum)+"' style='background-color:#F9FBFC' class='actionTr' parentImg='menuImg_"+data[i]['id']+"'>";
					}else{
						actionTrHtml+="<tr rowno='"+(i+j+2+lastActionNum)+"' style='background-color:#FFFFFF' class='actionTr' parentImg='menuImg_"+data[i]['id']+"'>";
					}
					actionTrHtml+="<td>"+(i+j+2+lastActionNum)+"</td>";
					actionTrHtml+="<td><input type='checkbox' value='"+actionData['id']+"' class='actionTrCheckbox' /></td>";
					actionTrHtml+="<td><q style='padding: 0 10px'></q>";
					actionTrHtml+="<img	src='<%=request.getContextPath()%>/images/butNoChild.gif'>&nbsp;";
					actionTrHtml+="<img	align='absmiddle' src='<%=request.getContextPath()%>/"+actionData['icon']+"'>&nbsp;";
					actionTrHtml+=actionData['actionName']+"</td>";//菜单名称
					actionTrHtml+="<td>"+actionData['actionCode']+"</td>";//菜单编码
					if(actionData['viewMode']=='Y'){//是否可见
						actionTrHtml+="<td><input type='checkbox' value='"+actionData['id']+"' disabled checked /></td>";
						}else{
						actionTrHtml+="<td><input type='checkbox' value='"+actionData['id']+"' disabled /></td>";
						}
					actionTrHtml+="<td><img	src='<%=request.getContextPath()%>/"+ actionData['icon'] + "'></td>";//ICON
					actionTrHtml += "<td>" + actionData['path']+ "</td>";//URL
					actionTrHtml += "<td>"+ actionData['refreshTime']+ "</td>";//URL
					actionTrHtml += "<td>"+ actionData['remark'] + "</td>";//REMARK
					actionTrHtml += "</tr>";
					menuTrHtml += actionTrHtml;
								}
								lastActionNum += data[i]['actions'].length;
								$(menuTrHtml).appendTo($("#menuData").find('tbody'));
								$(".dataTableHead").siblings().unbind('mouseover').bind('mouseover',function() {
									//lastColor=$(this).css("background-color");
									//$(this).css({"background-color":""}).addClass('jqgrowHover');
								});
								$(".dataTableHead").siblings().unbind('mouseout').bind('mouseout',function() {
									//$(this).removeClass('jqgrowHover').css({"background-color":lastColor});
								});
								$(".dataTableHead").siblings().unbind('click').bind('click',function() {
									if (!$(this).attr('selected')) {
										$(this).find(".menuTrCheckbox").attr('checked',true);
										$(this).find(".actionTrCheckbox").attr('checked',true);
										$(this).css({"background-color":"#D8F79D"}).attr('selected',true);
									} else {
										$(this).find(".menuTrCheckbox").removeAttr('checked');
										$(this).find(".actionTrCheckbox").removeAttr('checked');
										if($(this).attr('rowno')%2==0){
											$(this).removeAttr('selected').css({"background-color":"#FFFFFF"});
										}else{
											$(this).removeAttr('selected').css({"background-color":"#F9FBFC"});
										}
									}
									$(this).siblings().not(".dataTableHead").each(function(index,obj){
										$(this).find(".menuTrCheckbox").removeAttr('checked');
										$(this).find(".actionTrCheckbox").removeAttr('checked');
										if($(this).attr('rowno')%2==0){
											$(this).removeAttr('selected').css({"background-color":"#FFFFFF"});
										}else{
											$(this).removeAttr('selected').css({"background-color":"#F9FBFC"});
										}
										
									});
								});
							}
						});
	}
	function clickAllSelect(obj) {
		if ($(obj).attr('checked')) {
			$("#menuData :checkbox").attr('checked', 'true');
		} else {
			$("#menuData :checkbox").removeAttr('checked');
		}
	}
</script>
</head>
<body>
	<div class="dataGridTopDiv">
		<table
			style="width: 99%; border-collapse: separate; border-spacing: 6px; cellpadding: 0; cellspacing: 6; border: 0px solid #dddddd;">
			<tr valign="top">
				<td>
					<table width="100%" border="0" cellspacing="0" cellpadding="6"
						id="tableArea" class="blockTable">
						<tr>
							<td valign="middle" class="blockTd"><img
								src="<%=request.getContextPath()%>/images/icons/icon022a1.gif" />
								菜单列表</td>
						</tr>
						<tr>
							<td style="padding: 0 8px 4px;"><a id="newMenuBtn"
								href='javascript:void(0);' url="./MenuAdd.jsp" class='zPushBtn'
								tabindex='-1'><img
									src="<%=request.getContextPath()%>/images/icons/icon022a2.gif" /><b>新建&nbsp;</b></a>
								<a id="editMenuBtn"
								url="<%=request.getContextPath()%>/menu.do?method=updateMenu"
								href='javascript:void(0);' class='zPushBtn'><img
									src="<%=request.getContextPath()%>/images/icons/icon022a4.gif" /><b>编辑&nbsp;</b></a>
								<!-- 
								<a href='javascript:void(0);' class='zPushBtn'><img
									src="<%=request.getContextPath()%>/images/icons/icon022a16.gif" /><b>保存&nbsp;</b></a>
								<a href='javascript:void(0);' class='zPushBtn' id='delUserBtn'><img
									src="<%=request.getContextPath()%>/images/icons/icon400a8.gif" /><b>放弃&nbsp;</b></a>
									 -->
								<a href='javascript:void(0);' id="delMenuBtn" class='zPushBtn'
								url="<%=request.getContextPath()%>/menu.do?method=deleteMenu"><img
									src="<%=request.getContextPath()%>/images/icons/icon022a3.gif" /><b>删除&nbsp;</b></a>
								<a href='javascript:void(0);' class='zPushBtn' id='hideMenuBtn'><img
									src="<%=request.getContextPath()%>/images/icons/icon022a6.gif" /><b>隐藏&nbsp;</b></a>
								<a href='javascript:void(0);' class='zPushBtn' id='showMenuBtn'><img
									src="<%=request.getContextPath()%>/images/icons/icon022a7.gif" /><b>显示&nbsp;</b></a>
							</td>
						</tr>
						<tr>
							<td id="dataGridTd"
								style="padding-top: 0px; padding-left: 6px; padding-right: 6px; padding-bottom: 8px;">
								<table width="100%" cellspacing="0" cellpadding="2"
									id="menuData" class="dataTable">
									<tbody>
										<tr class="dataTableHead">
											<td width="3%"><img
												src="<%=request.getContextPath()%>/images/icons/icon_drag.gif"
												width="16" height="16"></td>
											<td width="2%"><input type='checkbox' id='selectAll' /></td>
											<td width="15%">菜单名称</td>
											<td width="15%">菜单编码</td>
											<td width="5%">是否可见</td>
											<td width="5%">ICON</td>
											<td width="25%">URL</td>
											<td width="20%">修改时间</td>
											<td width="20%">备注</td>
										</tr>
									</tbody>
								</table>
							</td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
	</div>
</body>
</html>
